/**
 * Created by Administrator on 2015/5/27.
 */
var DB = require('./db');

function Product(_id, name, pic, category, content) {

    this._id = _id;
    this.name = name;
    this.pic = pic;
    this.category = category;
    this.content = content;
};

Product.list = function (page, callback) {

    var queryOption = {};
    var keywordArray = [];

    //如果关键词不为空
    if (page.keyword && page.keyword !== '') {
        var regexp = new RegExp(page.keyword);
        keywordArray = [{"name": regexp},{"category": regexp}];
        queryOption['$or'] = keywordArray;
    }

    //如果分类不为空
    if (page.category && page.category !== '') {
        queryOption['category'] = page.category;
    }

    var total = 0;

    //当前是第几页
    var pageNumber = parseInt(page.pageNumber) || 1;

    //每页有几个
    var perNumber = parseInt(page.perNumber) || 10;

    //要跳过的数目
    var skipNumber = (pageNumber - 1) * perNumber;

    DB.connect(function (err, db) {
        if (err) {
            return callback(err, {});
        }

        var collection = db.collection('product');
        collection.count(queryOption, function (err, count) {
            if (err) {
                return callback(err, {});
            }
            total = count;

            collection.find(queryOption, {"content": 0})
                .sort({"_id": 1})
                .skip(skipNumber)
                .limit(perNumber)
                .toArray(function (err, docs) {
                    db.close();
                    if (docs.length) {
                        var returnPage = {};

                        returnPage.list = docs;
                        returnPage.pageNumber = pageNumber;
                        returnPage.keyword = queryOption.keyword;
                        returnPage.category = queryOption.category;

                        //如果当前为第一页的话，前一页为空
                        returnPage.prevpage = (pageNumber == 1) ? null : pageNumber - 1;

                        //总页数，如果当前页等于总页数的话，下一页则为空
                        var totalpage = Math.ceil(total/perNumber);
                        if (pageNumber == totalpage) {
                            returnPage.nextpage = null;
                        } else {
                            returnPage.nextpage = pageNumber + 1;
                        }

                        return callback(err, returnPage);

                    } else {
                        var returnPage = {};
                        returnPage.pageNumber = 1;
                        returnPage.keyword = queryOption.keyword;
                        returnPage.category = queryOption.category;
                        returnPage.prevpage = null;
                        returnPage.nextpage = null;
                        returnPage.list = {};
                        return callback(err, returnPage);
                    }
                })
            ;
        });
    })
};

Product.prototype.save = function (callback) {
    var product = {
        id: this.id,
        name: this.name,
        abstract: this.abstract,
        content: this.content,
        price: this.price,
        time: this.time
    }

    DB.connect(function (db) {
        var collection = db.collection('product');

        collection.insertOne(product, function (err, result) {
            if (err || result.result.n < 1) {
               return callback();
            }
            console.log('成功插入一个产品');
            db.close();
            return callback();
        })
    });
}


module.exports = Product;
